package neusoft.yuejian_11.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import neusoft.yuejian_11.activity.PostSearchActivity;
import neusoft.yuejian_11.activity.MylikepostActivity;
import neusoft.yuejian_11.adapter.PClubCollectAdapter;
import neusoft.yuejian_11.adapter.PostListAdapter;
import neusoft.yuejian_11.base.BaseFragment;
import neusoft.yuejian_11.bean.ClubCollectBean;
import neusoft.yuejian_11.bean.PostBean;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.AttentionModel;
import neusoft.yuejian_11.model.PostModel;
import neusoft.yuejian_11.R;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
//所有帖子列表（已改）
public class PostListFragment extends BaseFragment  implements RetrofitListener {
    private ImageView  addpostView;
    private ImageButton collectpostButton;
    private PClubCollectAdapter cadapter;
    private PostListAdapter padapter;
    List<String> list = new ArrayList<String>();
    private RecyclerView recyclerView;
    private  View view=null;
    private Spinner mySpinner;
    private TextView myTextView;
    private ArrayAdapter<String> adapter;
    private FragmentManager manager;
    public PostListFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_post_list,container,false);
        //跳转到添加晒图页
        addpostView= (ImageView) view.findViewById(R.id.addpost);
        addpostView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(),PostSearchActivity.class);
                startActivityForResult(intent,1 );//requestCode
            }
        });

        collectpostButton=(ImageButton) view.findViewById(R.id.collectpost);
        //跳转到收藏帖子列表页
        collectpostButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(),MylikepostActivity.class);
                startActivityForResult(intent,1 );//requestCode
            }
        });
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);

        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        list.add("全部晒图");
        list.add("关注俱乐部");
        myTextView=(TextView) view.findViewById(R.id.shaitu);
        mySpinner = (Spinner)  view.findViewById(R.id.setpiclist);
        adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mySpinner.setAdapter(adapter);

        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {//选择item的选择点击监听事件
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
                // TODO Auto-generated method stub



                if ("全部晒图"==adapter.getItem(arg2))

                {
                    myTextView.setText( adapter.getItem(arg2));
                    PostModel postModel=new PostModel();
                    postModel.getPostlist(PostListFragment.this);
                }



                else if ("关注俱乐部"==adapter.getItem(arg2))

                {
                    myTextView.setText( adapter.getItem(arg2));
                    AttentionModel attentionModel=new AttentionModel();
                    attentionModel.getClubCollectlist(getU_id(),"4",PostListFragment.this);
                }

            }


            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });


        return view;
    }
    private void replace(Fragment fragment) {
        manager = getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.setpiocinf, fragment);
        transaction.commit();
    }

    @Override
    public void onSuccess(Object o, int flag) {
            //显示所有晒图列表
//        System.out.println("==================="+flag);
        if (flag==1) {
            List<PostBean> duijiangBean = (List<PostBean>) o;
            padapter = new PostListAdapter(getActivity(), duijiangBean);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
            gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(gridLayoutManager);
            recyclerView.setAdapter(padapter);
            recyclerView.addItemDecoration(new SpaceItemDecoration(8));
            recyclerView.setItemAnimator(new DefaultItemAnimator());
        }
        if (flag==2){
            List<ClubCollectBean> clubCollectBean = (List<ClubCollectBean>) o;
            cadapter=new PClubCollectAdapter(getActivity(),clubCollectBean);
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(cadapter);
            recyclerView.addItemDecoration(new SpaceItemDecoration(1));
            recyclerView.setItemAnimator(new DefaultItemAnimator());

        }

    }

    @Override
    public void onFail() {

    }
}
